package com.guoheng.hazard.sys.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.guoheng.hazard.common.base.Result;
import com.guoheng.hazard.common.enums.ErrorCodeEnum;
import com.guoheng.hazard.common.util.StringUtils;
import com.guoheng.hazard.sys.mapper.LoginMapper;
import com.guoheng.hazard.sys.mapper.entpr.EntprBasicInfoMapper;
import com.guoheng.hazard.sys.model.dto.LoginDTO;
import com.guoheng.hazard.sys.model.dto.ReturnLoginDTO;
import com.guoheng.hazard.sys.model.dto.entpr.EntprBasicInfoSelectDTO;
import com.guoheng.hazard.sys.model.dto.entpr.EntprScopeDTO;
import com.guoheng.hazard.sys.model.entity.SysUser;
import com.guoheng.hazard.sys.model.entity.entpr.EntprBasicInfo;
import com.guoheng.hazard.sys.service.LoginService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 类功能描述:　用户服务层
 *
 * @author fbl
 * @date 2019-37-11 14:37
 */
@Service
public class LoginServiceImpl implements LoginService {

    @Resource
    private LoginMapper loginMapper;

    @Resource
    EntprBasicInfoMapper entprBasicInfoMapper;

    /**
     * 方法功能描述: 用户登录
     *
     * @param loginDTO 登录参数
     * @return Result
     */
    @Override
    public Result userLogin(LoginDTO loginDTO) {
        // 根据账号查询用户
        SysUser user = loginMapper.selectByAccount(loginDTO.getUsername());
        if (user != null) {
            // 查询密码是否正确
            if (loginDTO.getPassword().equals(user.getPassword())) {
                ReturnLoginDTO returnLoginDTO = new ReturnLoginDTO();
                // 查询用户企业信息
                Long entprId = user.getEntprId();
                if (entprId != null) {
                    EntprBasicInfo entprBasicInfo = entprBasicInfoMapper.getEntprBasicInfoById(entprId);
                    returnLoginDTO.setUser(user);
                    EntprBasicInfoSelectDTO entprBasicInfoSelectDTO = new EntprBasicInfoSelectDTO();
                    BeanUtils.copyProperties(entprBasicInfo, entprBasicInfoSelectDTO);
                    String entprScope = entprBasicInfo.getEntprScope();
                    transitionEntprScope(entprScope,entprBasicInfoSelectDTO);
                    returnLoginDTO.setEntprBasicInfo(entprBasicInfoSelectDTO);
                    return Result.success(returnLoginDTO);
                } else {
                    return Result.success();
                }
            }
        }
        return Result.failure(ErrorCodeEnum.SYS_ERR_LOGIN_FAIL);
    }

    public void transitionEntprScope(String entprScope, EntprBasicInfoSelectDTO entprBasicInfoSelectDTO) {
        if (StringUtils.isNotEmpty(entprScope)) {
            entprScope = "{\"data\":" + entprScope + "}";
            JSONObject jsonObject = JSON.parseObject(entprScope);
            JSONArray jsonArray = jsonObject.getJSONArray("data");
            List<EntprScopeDTO> entprScopeDTOS = jsonArray.toJavaList(EntprScopeDTO.class);
            entprBasicInfoSelectDTO.setEntprScope(entprScopeDTOS);
        } else {
            entprBasicInfoSelectDTO.setEntprScope(new ArrayList<EntprScopeDTO>());
        }

    }
}
